require 'fileutils'

module Intranet
  module FileSystem
    include Security::Session::Access
    protected
      #Directory definitions
      def container_dir
        RAILS_ROOT +  "/public/"
      end

      def documents_dir
        "#{container_dir}documents/"
      end

      def relative_documents_dir
        "/documents/"
      end

      #Directory methods
      def create_documents_dir
        FileUtils.mkdir_p(documents_dir,:mode => 0777) unless documents_dir_exists?
      end

      def delete_documents_dir(category)
        return if category.nil?
        ddir = documents_dir + category
        tdir = documents_dir + category + "/tmp"
        FileUtils.rmdir(tdir) if File.exist?(tdir)
        FileUtils.rmdir(ddir) if File.exist?(ddir)
      end

      def documents_dir_exists?
        File.exist?(documents_dir)
      end

      # File Utilities
      def documents_dir_has_content?(content)
        #content can either be a file or another directory
        return false unless documents_dir_exists?
        File.exist?(documents_dir + content)
      end
  end#filesystem module
end
